package nl.utwente.fmt.vertalerbouw.tarrh;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import org.antlr.runtime.RecognitionException;
import org.apache.commons.io.FilenameUtils;

/**
 * Application that compiles a Tarrh source file and writes the resulting java
 * bytecode to class files.
 */
public class TarrhCompiler {
	/**
	 * Application that compiles a Tarrh source file and writes the resulting
	 * java bytecode to class files.
	 * 
	 * @param args
	 *            the arguments to the application.
	 * @throws IOException
	 *             if the specified files cannot be read or written.
	 * @throws RecognitionException
	 *             if an error occurs during parsing, checking or encoding.
	 */
	public static void main(String[] args) throws IOException, RecognitionException {
		File inputFile = null;
		File outputDirectory = null;
		if (args.length == 2) {
			inputFile = new File(args[0]);
			outputDirectory = new File(args[1]);
		} else {
			System.err.println("usage: TarrhCompiler source-file output-directory");
			System.exit(1);
		}
		Reader inputReader = new FileReader(inputFile);
		String className = FilenameUtils.removeExtension(inputFile.getName());
		TarrhUtils.encode(className, inputReader).writeClassFilesToDirectory(outputDirectory);
		inputReader.close();
	}
}
